package com.ruyuan.seckilling.common;

/**
 * @author zhonghuashishan
 */
public interface LuaScript {

    String CONFIM_STOCK = "local stockKey = '%s';"
            + "local lockedStock = redis.call('hget', stockKey, 'lockedStock') + 0;"
            + "local saledStock = redis.call('hget', stockKey, 'saledStock') + 0;"
            + "redis.call('hset', stockKey, 'lockedStock', lockedStock - 1);"
            + "redis.call('hset', stockKey, 'saledStock', saledStock + 1);";

    String RELEASE_STOCK = "local stockKey = '%s';"
            + "local salableStock = redis.call('hget', stockKey, 'salableStock') + 0;"
            + "local lockedStock = redis.call('hget', stockKey, 'lockedStock') + 0;"
            + "redis.call('hset', stockKey, 'salableStock', salableStock + 1);"
            + "redis.call('hset', stockKey, 'lockedStock', lockedStock - 1);";


    static String buildConfirmStockScript(String key) {
        return String.format(CONFIM_STOCK, key);
    }

    static String buildReleaseStockScript(String key) {
        return String.format(RELEASE_STOCK, key);
    }
}
